package com.huike.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huike.pojo.Bed;
import com.huike.pojo.HospInfo;
import com.huike.pojo.Patient;
import com.huike.pojo.UpdatePatient;
import com.huike.service.PatientService;
import com.huike.vo.ResultVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("patient")
public class PatientController {
    @Autowired
    private PatientService patientService;


    @RequestMapping("list")
    @ResponseBody
    public PageInfo getAllPatient(Integer id,String doctorName,String dept,
                                  @RequestParam(value= "pageNum",defaultValue = "1") Integer pageNum,
                                  @RequestParam(value= "pageSize",defaultValue = "5")Integer pageSize){

        PageHelper.startPage(pageNum,pageSize);

        List<Patient> list =  patientService.selectAllPatient(id,doctorName,dept);

        PageInfo pageInfo=new PageInfo(list,5);

        return pageInfo;
    }

    @RequestMapping("getPatientDetailById")
    @ResponseBody
    public ResultVO<Patient> getPatientDetailById(int id){
        List<Patient> patients = patientService.getPatientDetailById(id);
        return new ResultVO<>(patients);
    }

    @RequestMapping("updatePatient")
    @ResponseBody
    public ResultVO<Patient> updatePatient(Patient patient){
        try{
            patientService.updatePatient(patient);
            return new ResultVO(200,"修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return new ResultVO(300,"修改失败");
        }
    }


    @RequestMapping("updatePatientAndBed")
    @ResponseBody
    public ResultVO updatePatientAndBed(UpdatePatient updatePatient){
        try{
            patientService.updatePatientAndBed(updatePatient);
            return new ResultVO(200,"修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return new ResultVO(300,"修改失败");
        }
    }

    @RequestMapping("inHospital")
    @ResponseBody
    public PageInfo getAllInHospital(Integer id,String patientName,String dept,
                                     @RequestParam(value= "pageNum",defaultValue = "1") Integer pageNum,
                                     @RequestParam(value= "pageSize",defaultValue = "5")Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);

        List<Bed> list =  patientService.getAllInHospital(id,patientName,dept);

        PageInfo pageInfo=new PageInfo(list,5);

        return pageInfo;
    }

    @RequestMapping("forHospital")
    @ResponseBody
    public ResultVO<Bed> getBedById(int id){
        List<Bed> beds = patientService.getBedById(id);
        return new ResultVO<>(beds);
    }

    @RequestMapping("beInHospital")
    @ResponseBody
    public ResultVO beInHospital(int id){
        try{
            patientService.beInHospital(id);
            return new ResultVO(200,"入院成功");
        }catch (Exception e){
            return new ResultVO(300,"入院失败");
        }
    }

    @RequestMapping("outHospital")
    @ResponseBody
    public ResultVO outHospital(int id){
        try{
            patientService.outHospital(id);
            return new ResultVO(200,"入院成功");
        }catch (Exception e){
            return new ResultVO(300,"入院失败");
        }
    }

    @RequestMapping("addPatient")
    @ResponseBody
    public ResultVO addPatient(Patient patient){
        try{
            patientService.addPatient(patient);
            return new ResultVO(200,"添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return new ResultVO(300,"添加失败");
        }
    }

    @RequestMapping("delete/{id}")
    @ResponseBody
    public ResultVO<Void> deleteById(@PathVariable("id")Integer id){
        try{
            patientService.deleteById(id);
            return new ResultVO(200,"删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return new ResultVO(300,"删除失败");
        }
    }

    @RequestMapping("getPatientById")
    @ResponseBody
    public ResultVO<Bed> getPatientById(int id){
        Bed bed = patientService.getPatientById(id);
        return new ResultVO<>(bed);
    }

//    @RequestMapping("bedInfo")
//    @ResponseBody
//    public ResultVO<>
}
